package com.dinadi.yidi.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.ProductAattributesBean;
import com.dinadi.yidi.bean.ProductIDRequestEntity;
import com.dinadi.yidi.bean.SkuListBean;
import com.dinadi.yidi.ui.view.SKUViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/7/14
 * Time: 18:05
 */
public class ProductInformationAdapter extends BaseAdapter {

    private Context context;
    private List<ProductIDRequestEntity> mList = new ArrayList<>();
    private List<SkuListBean> skuList = new ArrayList<>();
    private TextView textView;
    private ProductAattributesBean[] selectList;
    private View.OnClickListener mListener;
    boolean isSku = true;


    public ProductInformationAdapter(Context context, List<ProductIDRequestEntity> mList, List<SkuListBean> skuListBeen) {
        this.context = context;
        this.mList = mList;
        this.skuList = skuListBeen;
        selectList = new ProductAattributesBean[mList.size()];
    }

    public void notifyDataSetChanged() {
        if (mList != null && mList.size() > 0 &&
                (selectList == null || selectList.length != mList.size())) {
            selectList = new ProductAattributesBean[mList.size()];
        }
        super.notifyDataSetChanged();
    }

    public void addListener(View.OnClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.adapter_product_information_item, null); /**这个是评论的item列表*/
            viewHolder = new ViewHolder();
            viewHolder.tv_product_attribute = (TextView) convertView.findViewById(R.id.tv_product_attribute);
            viewHolder.vg_skuItem = (SKUViewGroup) convertView.findViewById(R.id.main_rl);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        /*属性*/
        viewHolder.tv_product_attribute.setText(mList.get(position).getAttrName());
        /*属性值*/
        List<ProductAattributesBean> childrens = mList.get(position).getItems();
        int childrenSize = childrens.size();
        viewHolder.vg_skuItem.removeAllViews();
        TextView[] textViews = new TextView[childrenSize];
        for (int i = 0; i < childrenSize; i++) {
            Log.d("@@@@@@@@@@@@@@@@@@@@","~~~~~~~~~~`");
            final ProductAattributesBean bean = childrens.get(i);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 30, 30, 0);
            textView = new TextView(context);
            textView.setGravity(Gravity.CENTER);
            textView.setPadding(30, 5, 30, 5);
            textView.setLayoutParams(params);
            textView.setBackgroundResource(R.drawable.hei_border);
            textView.setText(childrens.get(i).getAttrValue());
            textView.setTextColor(ContextCompat.getColor(context, R.color.color_333333));
            textViews[i] = textView;
            viewHolder.vg_skuItem.addView(textViews[i]);
            textView.setTag(R.id.attrValuePosition, position);
            textView.setTag(R.id.attrValue, bean);
            textView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    /*点击的时候取获属性的位置*/
                    int p = (int) v.getTag(R.id.attrValuePosition);
                     /*点击的时候获取属性值*/
                    ProductAattributesBean b = (ProductAattributesBean) v.getTag(R.id.attrValue);
                    if (selectList[p] != null && selectList[p].getAttrValue().equals(b.getAttrValue())) {
                        /*取消选中*/
                        selectList[p] = null;
                    } else {
                        /*选中*/
                        selectList[p] = b;
                    }
                    if (mListener != null) {
                        v.setTag(R.id.attrValueArray, selectList);
                        mListener.onClick(v);
                    }


                }

            });
            if (mList.size() > 0) {
                ArrayList<String> strList = new ArrayList<>();
                isSku = false;

                for (int selectIndex = 0; selectIndex < selectList.length; selectIndex++) {
                    //循环选中的集合
                    ProductAattributesBean selectBean = selectList[selectIndex];
                    if (selectIndex == position) {
                        strList.add(bean.getAttrValueId());
                    } else if (selectBean != null && !TextUtils.isEmpty(selectBean.getAttrValueId())) {
                        strList.add(selectBean.getAttrValueId());
                    }
                }
                for (SkuListBean skuListBean : skuList) {//这个地方的冒号就是遍历skuList的集合，取出每一个元素
                    /*所有属性分组循环*/
                    boolean flag = true;
                    for (String key : strList) {//遍历你选择的属性集合strList，取出每一个元素key
                        /*所有带KEY值的属性分组循环*/
                        if (!skuListBean.getAttrValueIds().contains(key) ||
                                TextUtils.isEmpty(skuListBean.getStock()) ||
                                skuListBean.getStock().equals("0")) {
                            /*如果所有分组里边不包含KEY || 库存为0   所以没有库存*/
                            flag = false;
                            break;
                        } else {
                            textView.setTag(R.id.stock, skuListBean);
                        }
                    }
                    if (flag) {
                        isSku = true;
                        break;
                    }
                }
                if (!isSku) {
                    /*如果没有库存*/
                    textView.setOnClickListener(null);
                    textView.setBackgroundResource(R.drawable.hui_border);
                    textView.setTextColor(ContextCompat.getColor(context, R.color.color_999999));
                } else {
                    textView.setBackgroundResource(R.drawable.hei_border);
                    textView.setTextColor(ContextCompat.getColor(context, R.color.color_333333));
                    if (selectList[position] != null && bean.getAttrValueId().equals(selectList[position].getAttrValueId())) {
                        /*如果选中的集合不等于空并且bean中的getAttrValueId等于选中的id*/
                        textView.setBackgroundResource(R.drawable.fen_border);
                        textView.setTextColor(ContextCompat.getColor(context, R.color.color_F91D48));}
                }
            }
        }


        return convertView;
    }


    class ViewHolder {
        TextView tv_product_attribute;
        SKUViewGroup vg_skuItem;
    }

}
